<?php

/**
 * @package Core
 */
class Core_Stomp {

    protected static $instance;
    /**
     * @var Stomp
     */
    protected $conn;
    public static $timeout = 3;

    /**
     *
     * @param string $name
     * @return Core_Stomp
     */
    public static function newInstance($name = 'default') {
        return new Core_Stomp($name);
    }

    /**
     *
     * @param string $name
     * @return Core_Stomp
     */
    public static function getInstance($name = 'default') {
        if (empty(self::$instance[$name])) {
            self::$instance[$name] = new Core_Stomp($name);
        }

        return self::$instance[$name];
    }

    private function __construct($configName = 'default') {
        $config = Core_Config::get("jmsServer.{$configName}");
        $this->conn = new Stomp('tcp://' . $config['host'] . ':' . $config['port']);
        $this->conn->setReadTimeout(self::$timeout);
    }

    public function send($body, $queueName) {
        return $this->conn->send($queueName, json_encode($body));
    }

    public function subscribe($queueName) {
        return $this->conn->subscribe($queueName);
    }

    public function unsubscribe($queueName) {
        return $this->conn->unsubscribe($queueName);
    }

    public function hasFrame() {
        return $this->conn->hasFrame();
    }

    public function readFrame() {
        return $this->conn->readFrame();
    }

    public function ack($message) {
        return $this->conn->ack($message);
    }

}